<?php
/**
 * includes/functions.php
 *
 * Shorthand functions
 *
 * Author: BCG
 *
 */

//
//	shorthands for lazy people
//

	function host(){ # returns HTTP_HOST with any port numbers removed
		return(preg_replace('/:.+$/',"",$_SERVER["HTTP_HOST"])); }
	function is_https(){ # return true or false, HTTPS enabled
		global $https_key,$https_value;
		return(isset($_SERVER[$https_key]) && $https_value === $_SERVER[$https_key] ? true : false); }
	function _post($key){ # safely return escaped value from $_POST array
		return(isset($_POST[$key]) ? attribute_escape($_POST[$key]) : null); }
	function redirect_to(){ # return WordPress' redirect_to
		return(isset($_REQUEST["redirect_to"]) ? attribute_escape($_REQUEST["redirect_to"]) : ""); }
	function req_uri(){ # return server request uri
		return($_SERVER["REQUEST_URI"]); }
	function scheme($use_https){ # return scheme based on test value
		return(($use_https ? "https" : "http")."://"); }
	function user_can($what){ # checks if function exists before calling it
		return(function_exists("current_user_can") ? current_user_can($what) : false); }

//
//	safe redirect function - don't want to use wp_redirect()
//

	function as_redirect($location)
	{
		if((!defined("TEST") || $first_test) && !defined("ADMIN_SSL_DO_NOT_REDIRECT"))
		{
			session_write_close();
			header("location: $location");
			exit;
		}
		elseif(TEST) return($location);
	}

//
//	get the Apache version
//

	function apache_version($test=null, $precision=0)
	{
		$re = "/Apache\/([\d.]+)/";
		preg_match($re, $_SERVER["SERVER_SOFTWARE"], $matches);
		$version = $matches[1];

		if(is_null($test)) return(doubleval($version));
		else return(round($version, $precision) == $test ? true : false);
	}
?>